package com.insurance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.insurance.domain.ResponseResult;
import com.insurance.domain.entity.Customer;
import com.insurance.domain.entity.Guarantee;
import com.insurance.domain.enums.AppHttpCodeEnum;
import com.insurance.domain.vo.GuaranteePageVo;
import com.insurance.domain.vo.PageVo;
import com.insurance.mapper.GuaranteeMapper;
import com.insurance.service.GuaranteeService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * (Guarantee)表服务实现类
 *
 * @author makejava
 * @since 2023-04-22 09:50:14
 */
@Service("guaranteeService")
public class GuaranteeServiceImpl extends ServiceImpl<GuaranteeMapper, Guarantee> implements GuaranteeService {

    @Override
    public ResponseResult listByPage(GuaranteePageVo guaranteePageVo) {
        LambdaQueryWrapper<Guarantee> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.like(!ObjectUtils.isEmpty(guaranteePageVo.getInsured()),Guarantee::getInsured,guaranteePageVo.getInsured());
        queryWrapper.eq(!ObjectUtils.isEmpty(guaranteePageVo.getEffectivedate()),Guarantee::getEffectivedate,guaranteePageVo.getEffectivedate());
        queryWrapper.like(!ObjectUtils.isEmpty(guaranteePageVo.getInsuranceName()),Guarantee::getInsuranceName,guaranteePageVo.getInsuranceName());
        queryWrapper.like(!ObjectUtils.isEmpty(guaranteePageVo.getInsuranceCategory()), Guarantee::getInsuranceCategory,guaranteePageVo.getInsuranceCategory());
        Page<Guarantee> page = new Page<>(guaranteePageVo.getPageNo(), guaranteePageVo.getPageSize());
        page(page,queryWrapper);
        List<Guarantee> guarantees = page.getRecords();
        PageVo pageVo = new PageVo(guarantees, page.getTotal());
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult addGuarantee(Guarantee guarantee) {
        if(save(guarantee)){
            return ResponseResult.okResult();
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.ADD_FAILED);
    }

    @Override
    public ResponseResult updateGuarantee(Guarantee guarantee) {
        if(updateById(guarantee)){
            return ResponseResult.okResult();
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.UPDATE_ERROR);
    }
}
